package com.javaee.xiangyang.controller.Admin;

import com.javaee.xiangyang.dto.GoodsDetails;
import com.javaee.xiangyang.entity.Classify;
import com.javaee.xiangyang.entity.Comment;
import com.javaee.xiangyang.entity.Goods;
import com.javaee.xiangyang.entity.enmu.impl.CommentState;
import com.javaee.xiangyang.service.CommentService;
import com.javaee.xiangyang.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/Commodity")
public class CommodityController {

    @Resource
    GoodsService goodsService;

    @Resource
    CommentService commentService;

    @RequestMapping("/Management")
    public ModelAndView getGoodsList(ModelAndView modelAndView) {
        //首先获取所有分类
        List<Classify> allClassify = goodsService.findAllClassify();
        //其次获取所有商品及其评论
        List<GoodsDetails> allGoods = goodsService.findAllGoodsDetails();
        //传递值
        modelAndView.addObject("allClassify", allClassify);
        modelAndView.addObject("allGoods", allGoods);
        modelAndView.setViewName("/admin/product_list");
        return modelAndView;
    }

    /**
     * 管理员增加商品
     */
    @RequestMapping("/AddGoods")
    @Transactional
    public ModelAndView addGoods(ModelAndView modelAndView, MultipartFile file, Goods goods, int goodsClassify) throws IOException {
        Classify classify = new Classify();
        classify.setClassifyId(goodsClassify);
        goods.setClassify(classify);
        //保存文件
        String filename = file.getOriginalFilename();//这个是文件名称
        file.transferTo(new File("E:\\JSP\\xiangyang\\src\\main\\resources\\static\\Images\\goodsImages\\" + filename));
        goods.setGoodsImg("/goodsImages/" + filename);
        goodsService.addGoods(goods);
        modelAndView.setViewName("redirect:/Commodity/Management");
        return modelAndView;
    }

    /**
     * 管理员前往增加商品界面
     */
    @RequestMapping("/toAddGoods")
    public ModelAndView toAddGoods(ModelAndView modelAndView) {
        //获取分类信息
        List<Classify> allClassify = goodsService.findAllClassify();
        //跳转页面
        modelAndView.addObject("allClassify", allClassify);
        modelAndView.setViewName("/admin/product_detail");
        return modelAndView;
    }

    /**
     * 管理员查找
     * 模糊查询
     */
    @RequestMapping("/Something")
    public ModelAndView getGoodListBySomething(ModelAndView modelAndView, Classify classify, String text) {
        //获取选中的分类编号
        int classifyId = classify.getClassifyId();
        //获取所有分类
        List<Classify> allClassify = goodsService.findAllClassify();
        //获取所查找到的商品及其评论
        List<GoodsDetails> goodsBySomething = goodsService.findGoodsBySomething(text, classify);
        modelAndView.addObject("classifyId", classifyId);
        modelAndView.addObject("allClassify", allClassify);
        modelAndView.addObject("allGoods", goodsBySomething);
        modelAndView.addObject("string", text);
        modelAndView.setViewName("/admin/product_list");
        return modelAndView;
    }

    /**
     * 更改商品评论状态
     */
    @RequestMapping("/UpdateState")
    public ModelAndView updateCommentState(ModelAndView modelAndView, Comment comment) {
        commentService.updateStateByCommentId(comment);
        modelAndView.setViewName("redirect:/Commodity/Management");
        return modelAndView;
    }

    /**
     * 前往商品信息修改页面
     */
    @RequestMapping("/Update")
    public ModelAndView getGoods(ModelAndView modelAndView, Goods goods) {
        GoodsDetails goodsDetails = goodsService.findGoodsDetails(goods);
        modelAndView.addObject("goods", goodsDetails.getGoods());
        modelAndView.setViewName("/admin/product_edit");
        return modelAndView;
    }

    /**
     * 更改商品信息
     * 商品名称
     * 商品价格
     * 商品图片
     * 商品简介
     */
    @RequestMapping("/UpdateGoods")
    public ModelAndView updateGoods(ModelAndView modelAndView, MultipartFile file, Goods goods) throws IOException {
        GoodsDetails goodsDetails = goodsService.findGoodsDetails(goods);
        if (!file.getOriginalFilename().equals("")) {
            //保存文件
            String filename = file.getOriginalFilename();//这个是文件名称
            file.transferTo(new File("E:\\JSP\\xiangyang\\src\\main\\resources\\static\\Images\\goodsImages\\" + filename));
            goods.setGoodsImg("/goodsImages/" + filename);
        } else {
            goods.setGoodsImg(goodsDetails.getGoods().getGoodsImg());
        }
        goodsService.updateCommentByCommentId(goods);
        modelAndView.setViewName("redirect:/Commodity/UpdateState");
        return modelAndView;
    }

    /**
     * 前往商品评论详情页面
     */
    @RequestMapping("/ToComment")
    public ModelAndView toComment(ModelAndView modelAndView, Goods goods) {
        GoodsDetails goodsDetails = goodsService.findGoodsDetails(goods);
        modelAndView.addObject("goods", goodsDetails);
        modelAndView.setViewName("/admin/pinglun");
        return modelAndView;
    }

}
